﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using COMOSOFT.WareHouse_Management.Class;

namespace COMOSOFT.WareHouse_Management.Forms
{
    public partial class FrmImportProduct : Form
    {
        ClassShare clsShare = new ClassShare("FrmImportProduct");        

        List<object> ListItem=new List<object>();

        int intPrIID;
        int intOID;
        double dblQty;
        double dblUnitPrice;

        public FrmImportProduct(int intPrIID)
        {
            InitializeComponent();
            this.intPrIID = intPrIID;
        }

        #region [Load Data]
        public void LoadData()
        {
            try
            {
                factoryListTableAdapter.Fill(this.dataSet_Product.FactoryList); //Load Factory List
                employeeTableAdapter.Fill(this.dataSet_Product.Employee);//Load Employee List
                if (intPrIID > 0)
                {
                    //created Master Info
                    product_ImportTableAdapter.Fill(this.dataSet_Product.Product_Import, intPrIID);
                    sp_ProductImportDetailsTableAdapter.Fill(dataSet_Product.sp_ProductImportDetails, intPrIID);
                    //Enabled group Details
                    if (groupControl_DetalsInfo.Enabled == false)
                    {
                        groupControl_DetalsInfo.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
        #endregion

        #region[Add New Procut Improt]
        public void AddNew()
        {
            productImportBindingSource.AddNew();
            txtIssuaDate.EditValue = DateTime.Now;
            txtCreator.EditValue =Convert.ToInt64(ClassShare.Usid);
        }
        #endregion

        #region[Update Product Import]
        //strCom is command Update (Update or Delete)
        public void UpdateProductImport(string strCom)
        {
            try
            {
                if (strCom == "Update")
                {
                    //Update
                    productImportBindingSource.EndEdit();
                    product_ImportTableAdapter.Update(dataSet_Product.Product_Import);
                    dataSet_Product.Product_Import.AcceptChanges();

                    //Set ID and set text
                    intPrIID = Convert.ToInt32(txtPRIID.EditValue);
                    this.Text = "Import No: " + txtImportNo.EditValue.ToString();
                    if (groupControl_DetalsInfo.Enabled == false)
                    {
                        groupControl_DetalsInfo.Enabled = true;
                    }
                    //Insert Details
                    if (ListItem.Count > 0)
                    {
                        intOID = Convert.ToInt32(ListItem[0]);
                        dblQty = Convert.ToDouble(ListItem[1]);
                        dblUnitPrice = Convert.ToDouble(ListItem[2]);

                        clsShare.UpdateData("sp_ImportProductDetaisInsert", new string[] { "@PRIID", "@OID", "@Qty", "@UnitPrice" }, new object[] {intPrIID,intOID,dblQty,dblUnitPrice });
                        LoadData();
                    }
                }
                else
                {
                    //Delete
                    if ((MessageBox.Show("Are you sure delete item selected?", "Delettion...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                    {
                        dataSet_Product.Product_Import.Rows[0].Delete();
                        product_ImportTableAdapter.Update(dataSet_Product.Product_Import);
                        dataSet_Product.Product_Import.AcceptChanges();
                        this.Close();
                    }
                }
                product_ImportTableAdapter.Fill(dataSet_Product.Product_Import, intPrIID); // Load Master Info   
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
        #endregion

        public void SetValue(List<object> ListItem)
        {
            this.ListItem.Clear();
            this.ListItem = ListItem;
        }      
    }
}
